package com.youlai.boot.system.controller;

import com.youlai.boot.common.result.Result;
import com.youlai.boot.common.result.ResultCode;
import com.youlai.boot.system.model.QB.Medicine;
import com.youlai.boot.system.model.QB.Source;
import com.youlai.boot.system.service.MedicineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/v1/medicine")
public class MedicineController {
    /**
     * 生药鉴定SOP
     */
    @Autowired
    private MedicineService medicineService;


    @PostMapping("/save")
    public Result<?> saveMedicine(@RequestBody Medicine medicine) {
        return Result.judge(medicineService.saveMedicine(medicine),"添加成功","添加失败");
    }
    @PostMapping("/all")
    public Result<List<Medicine>> getAllMedicine() {
        List<Medicine> documents = medicineService.getAllMedicine();
        Result<List<Medicine>> result;
        if (!documents.isEmpty()) {
            result = Result.success(documents);
            result.setMessage("查询设备成功");
            result.setCode(ResultCode.SUCCESS.getCode());
        } else {
            result = Result.failed(ResultCode.SYSTEM_EXECUTION_ERROR);
            result.setMessage("查询失败，请重新查询");
        }
        return result;
    }
    @DeleteMapping("/{id}")
    public  Result<?> deleteMedicine(@PathVariable Integer id){
        return Result.judge(medicineService.deleteMedicineById(id),"删除成功","删除失败");
    }
}
